//
// Created by lymk on 2021/8/7.
//

#include "GMath.h"

GMath::PointPolyRelation GMath::JudgeRelationBetweenPointAndRectangle(GMath::Point p, GMath::Rectangle rect) {
    float min_sp = 0.1;
    float left = rect.LeftUpper.X;
    float right = rect.BottomRight.X;
    float upper = rect.LeftUpper.Y;
    float bottom = rect.BottomRight.Y;
    //std::cout<<p.X<<' '<<p.Y<<"|"<<left<<' '<<right<<' '<<upper<<' '<<bottom<<"|";
    if(p.X > right  || p.X < left || p.Y < upper || p.Y > bottom)
        return GMath::OUTSIDE_THE_POLY;
    else{
        if(p.X==left || p.X==right || p.Y==upper|| p.Y==bottom)
            return GMath::ON_THE_POLY;
        return GMath::INSIDE_THE_POLY;
    }
}

GMath::PolygonRelation GMath::JudgeRelationAmongRectangles(GMath::Rectangle rectangle1, GMath::Rectangle rectangle2) {
    PointPolyRelation ppr1,ppr2,ppr3,ppr4;
    ppr1 = JudgeRelationBetweenPointAndRectangle(rectangle1.LeftUpper,rectangle2);//std::cout<<ppr1<<std::endl;
    ppr2 = JudgeRelationBetweenPointAndRectangle(rectangle1.BottomRight,rectangle2);//std::cout<<ppr2<<std::endl;
    ppr3 = JudgeRelationBetweenPointAndRectangle(Point(rectangle1.LeftUpper.X,rectangle1.BottomRight.Y),rectangle2);//std::cout<<ppr3<<std::endl;
    ppr4 = JudgeRelationBetweenPointAndRectangle(Point(rectangle1.BottomRight.X,rectangle1.LeftUpper.Y),rectangle2);//::cout<<ppr4<<std::endl;
    //std::cout<<ppr1<<' '<<ppr2<<' '<<ppr3<<' '<<ppr4<<std::endl;
    if((ppr1 == INSIDE_THE_POLY || ppr1 == ON_THE_POLY) &&
        (ppr2 == INSIDE_THE_POLY || ppr2 == ON_THE_POLY) &&
        (ppr3 == INSIDE_THE_POLY || ppr3 == ON_THE_POLY) &&
        (ppr4 == INSIDE_THE_POLY || ppr4 == ON_THE_POLY)){
        return GMath::BELONG_TO;
    }
    else if((ppr1 == OUTSIDE_THE_POLY || ppr1 == ON_THE_POLY) &&
            (ppr2 == OUTSIDE_THE_POLY || ppr2 == ON_THE_POLY) &&
            (ppr3 == OUTSIDE_THE_POLY || ppr3 == ON_THE_POLY) &&
            (ppr4 == OUTSIDE_THE_POLY || ppr4 == ON_THE_POLY)){

        ppr1 = JudgeRelationBetweenPointAndRectangle(rectangle2.LeftUpper,rectangle1);//std::cout<<ppr1<<std::endl;
        ppr2 = JudgeRelationBetweenPointAndRectangle(rectangle2.BottomRight,rectangle1);//std::cout<<ppr2<<std::endl;
        ppr3 = JudgeRelationBetweenPointAndRectangle(Point(rectangle2.LeftUpper.X,rectangle2.BottomRight.Y),rectangle1);//std::cout<<ppr3<<std::endl;
        ppr4 = JudgeRelationBetweenPointAndRectangle(Point(rectangle2.BottomRight.X,rectangle2.LeftUpper.Y),rectangle1);//std::cout<<ppr4<<std::endl;

        if((ppr1 == INSIDE_THE_POLY || ppr1 == ON_THE_POLY) &&
           (ppr2 == INSIDE_THE_POLY || ppr2 == ON_THE_POLY) &&
           (ppr3 == INSIDE_THE_POLY || ppr3 == ON_THE_POLY) &&
           (ppr4 == INSIDE_THE_POLY || ppr4 == ON_THE_POLY)){
            return GMath::CONTAIN;
        }else if((ppr1 == OUTSIDE_THE_POLY || ppr1 == ON_THE_POLY) &&
                 (ppr2 == OUTSIDE_THE_POLY || ppr2 == ON_THE_POLY)&&
                 (ppr3 == OUTSIDE_THE_POLY || ppr3 == ON_THE_POLY) &&
                 (ppr4 == OUTSIDE_THE_POLY || ppr4 == ON_THE_POLY)){//std::cout<<ppr1<<' '<<ppr2<<' '<<ppr3<<' '<<ppr4<<std::endl;
            return GMath::DISJOINT;
        }else
            return GMath::INTERSECT;

    }else
        return GMath::INTERSECT;
}
